package cn.sciento.farm.devicemanager.gateway.uplink.service.fertilizer.venturi;


import cn.sciento.farm.devicemanager.dao.entity.DevFertilizerChannel;
import cn.sciento.farm.devicemanager.dao.entity.DevWaterFertilizer;
import cn.sciento.farm.devicemanager.gateway.model.uplink.FertilizerUplinkPacket;
import cn.sciento.farm.devicemanager.gateway.uplink.service.fertilizer.FertilizerDisposeService;
import cn.sciento.farm.devicemanager.service.FertilizerChannelService;
import cn.sciento.farm.devicemanager.service.WaterFertilizerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

@Order(1)
@Service(value = "FertilizerDataDisposeService")
public class FertilizerDataDisposeService implements FertilizerDisposeService {

    @Autowired
    private WaterFertilizerService waterFertilizerService;

    @Autowired
    private FertilizerChannelService fertilizerChannelService;



    @Override
    public void handleUplinkPacket(String eui, FertilizerUplinkPacket uplinkPacket) {
        DevFertilizerChannel channel = fertilizerChannelService.getChannel(eui, uplinkPacket.getChannel());
        if (channel == null) {
            return;
        }
        DevWaterFertilizer deviceWaterFertilizer = waterFertilizerService.getByEui(eui);
        if (deviceWaterFertilizer == null) {
            return;
        }
        if (uplinkPacket.isSensorData()) {
            channel.setExitEc(uplinkPacket.getExitEC());
            channel.setExitPh(uplinkPacket.getExitPH());
            channel.setEntryPh(uplinkPacket.getEntryPH());
            channel.setEntryEc(uplinkPacket.getEntryEC());
            channel.setInstantFlow(uplinkPacket.getInstantFlow());
            channel.setTotalFlow(uplinkPacket.getTotalFlow());
            channel.setPressure(uplinkPacket.getPressure());
            fertilizerChannelService.updateById(channel);

        }else {
            channel.setFertilizerValveStatus(uplinkPacket.getFertilizeValve());
            channel.setStirMotorStatus(uplinkPacket.getStirringMotor());
            channel.setBoosterPumpStatus(uplinkPacket.getPump());
            fertilizerChannelService.updateById(channel);

        }

    }

    @Override
    public void updateOnline(String concentrator, int status) {
        waterFertilizerService.updateOnline(concentrator,status);
    }
}
